using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Common.Utilities.NetworkUtilities;

namespace Workflows.Components.FileManipulation
{
    /// <summary></summary>
    public partial class FileMoveEditor : Form, IComponentEditor 
    {
        private object _OwnerActivityNode;
        private FileMoveComponent _FileMover;

        /// <summary></summary>
        public FileMoveEditor()
        {
            InitializeComponent();
        }

        private void btn_BrowseSrcFiles_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg=new OpenFileDialog();
            dlg.Multiselect = true;
            if(dlg.ShowDialog()==DialogResult.OK)
            {
                this.txt_SrcPaths.Text = StringUtil.ToString(dlg.FileNames, ",");
            }
        }

        private void btn_BrowseTgtFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg=new FolderBrowserDialog();
            if(dlg.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {
                this.txt_TgtFolder.Text = NetworkShareMap.getUNCPath(dlg.SelectedPath);
            }
        }

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _OwnerActivityNode; }
            set { _OwnerActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(FileMoveComponent); }
        }

        public void LoadSettings(
            GlobalVariable[] globalVars,
            WorkflowComponentBase[] fromComponents, 
            object activityNode, WorkflowComponentBase wfComponent)
        {
            this._OwnerActivityNode = activityNode;
            this._FileMover = (FileMoveComponent) wfComponent;

            this.txt_SrcPaths.Text = string.Empty;
            if(this._FileMover.SrcFiles !=null && this._FileMover.SrcFiles.Count>0)
            {
                this.txt_SrcPaths.Text = StringUtil.ToString(this._FileMover.SrcFiles.ToArray(), ",");
            }
            this.txt_TgtFolder.Text = this._FileMover.TgtFolder;
        }

        public TestResult Test()
        {
            return new TestResult(true, string.Empty);
        }

        public void SaveSettings()
        {
            string[] srcFiles = StringUtil.Split(this.txt_SrcPaths.Text.Trim(), ",");
            if(srcFiles !=null && srcFiles.Length>0)
            {
                this._FileMover.SrcFiles=new List<string>();
                this._FileMover.SrcFiles.AddRange(srcFiles);
            }
            else
            {
                this._FileMover.SrcFiles = null;
            }
            this._FileMover.TgtFolder = this.txt_TgtFolder.Text;
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if(this.UpdateComponentSettingEvent!=null)
            {
                this.UpdateComponentSettingEvent(this._OwnerActivityNode, this._FileMover);
            }
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}